﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;

namespace DALL.SysManage
{
    public  class T_PUB_MATERIALTYPE
    {
        public T_PUB_MATERIALTYPE()
        { }

        /// <summary>
        /// 智能提示元素类型名称
        /// </summary>
        /// <returns></returns>
        public DataSet GetMaterialTypeName()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select DISTINCT TYPE_NAME FROM T_PUB_MATERIALTYPE ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        #region  Method

      	/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(Model.SysManage.T_PUB_MATERIALTYPE model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into T_PUB_MATERIALTYPE(");
            strSql.Append("TYPE_NAME,FALG,REMARK,PARENT_ID,ISUSED)");

			strSql.Append(" values (");
            strSql.Append("@TYPE_NAME,@FALG,@REMARK,@PARENT_ID,@ISUSED)");
			strSql.Append(";select @@IDENTITY");
			Database db = DatabaseFactory.CreateDatabase();
			DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
			db.AddInParameter(dbCommand, "TYPE_NAME", DbType.String, model.TYPE_NAME);
			db.AddInParameter(dbCommand, "FALG", DbType.Int32, model.FALG);
			db.AddInParameter(dbCommand, "REMARK", DbType.String, model.REMARK);
			db.AddInParameter(dbCommand, "PARENT_ID", DbType.Int32, model.PARENT_ID);
            db.AddInParameter(dbCommand, "ISUSED", DbType.Boolean, model.ISUSED);
			int result;
			object obj = db.ExecuteScalar(dbCommand);
			if(!int.TryParse(obj.ToString(),out result))
			{
				return 0;
			}
			return result;
		}
       /// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Model.SysManage.T_PUB_MATERIALTYPE model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update T_PUB_MATERIALTYPE set ");
			strSql.Append("TYPE_NAME=@TYPE_NAME,");
			strSql.Append("REMARK=@REMARK,");
			strSql.Append("PARENT_ID=@PARENT_ID,");
			strSql.Append("ISUSED=@ISUSED");
			strSql.Append(" where TYPE_ID=@TYPE_ID ");
			Database db = DatabaseFactory.CreateDatabase();
			DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
			db.AddInParameter(dbCommand, "TYPE_ID", DbType.Int32, model.TYPE_ID);
			db.AddInParameter(dbCommand, "TYPE_NAME", DbType.String, model.TYPE_NAME);
			db.AddInParameter(dbCommand, "FALG", DbType.Int32, model.FALG);
			db.AddInParameter(dbCommand, "REMARK", DbType.String, model.REMARK);
			db.AddInParameter(dbCommand, "PARENT_ID", DbType.Int32, model.PARENT_ID);
			db.AddInParameter(dbCommand, "ISUSED", DbType.Boolean, model.ISUSED);
			int rows=db.ExecuteNonQuery(dbCommand);

			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int TYPE_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_PUB_MATERIALTYPE set ");
            strSql.Append("ISUSED='0'");
            strSql.Append(" where TYPE_ID=@TYPE_ID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "TYPE_ID", DbType.Int32, TYPE_ID);
            int rows = db.ExecuteNonQuery(dbCommand);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

     	public Model.SysManage.T_PUB_MATERIALTYPE GetModel(int TYPE_ID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select TYPE_ID,TYPE_NAME,FALG,REMARK,PARENT_ID,ISUSED from T_PUB_MATERIALTYPE ");
			strSql.Append(" where TYPE_ID=@TYPE_ID ");
			Database db = DatabaseFactory.CreateDatabase();
			DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
			db.AddInParameter(dbCommand, "TYPE_ID", DbType.Int32,TYPE_ID);
			Model.SysManage.T_PUB_MATERIALTYPE model=null;
			using (IDataReader dataReader = db.ExecuteReader(dbCommand))
			{
				if(dataReader.Read())
				{
					model=ReaderBind(dataReader);
				}
			}
			return model;
		}

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public List<Model.SysManage.T_PUB_MATERIALTYPE> GetLibraryInListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select TYPE_ID,TYPE_NAME,FALG,REMARK,PARENT_ID,ISUSED ");
            strSql.Append(" FROM T_PUB_MATERIALTYPE ");
            if (strWhere!="")
            {
                strSql.Append("where "+strWhere);
                strSql.Append(" and ISUSED='1'");
            }
            else
            {
                strSql.Append("where ISUSED='1'");     
            }
            List<Model.SysManage.T_PUB_MATERIALTYPE> list = new List<Model.SysManage.T_PUB_MATERIALTYPE>();
            Database db = DatabaseFactory.CreateDatabase();
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
            }
            return list;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select TYPE_ID,TYPE_NAME,FALG,REMARK,PARENT_ID,ISUSED ");
            strSql.Append(" FROM T_PUB_MATERIALTYPE ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append("ISUSED='1'");
            Database db = DatabaseFactory.CreateDatabase();
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public List<Model.SysManage.T_PUB_MATERIALTYPE> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select TYPE_ID,TYPE_NAME,FALG,REMARK,PARENT_ID,ISUSED ");
            strSql.Append(" FROM T_PUB_MATERIALTYPE ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<Model.SysManage.T_PUB_MATERIALTYPE> list = new List<Model.SysManage.T_PUB_MATERIALTYPE>();
            Database db = DatabaseFactory.CreateDatabase();
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
            }
            return list;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public Model.SysManage.T_PUB_MATERIALTYPE ReaderBind(IDataReader dataReader)
        {
            Model.SysManage.T_PUB_MATERIALTYPE model = new Model.SysManage.T_PUB_MATERIALTYPE();
            object ojb;
            ojb = dataReader["TYPE_ID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.TYPE_ID = (int)ojb;
            }
            model.TYPE_NAME = dataReader["TYPE_NAME"].ToString();
            ojb = dataReader["FALG"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.FALG = (int)ojb;
            }
            model.REMARK = dataReader["REMARK"].ToString();
            ojb = dataReader["PARENT_ID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.PARENT_ID = (int)ojb;
            }
            ojb = dataReader["ISUSED"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ISUSED = (int)ojb;
            }
            return model;
        }

        #endregion  Method
    }
}
